<template>
  <div class="servicePage">
    <div class="configuration">
      <el-form
        ref="formRef"
        :model="ruleForm"
        :rules="rules"
        label-width="180px"
      >
        <el-form-item label="服务ID" prop="appId">
          <el-input v-model="ruleForm.appId" maxlength="20" />
        </el-form-item>
        <el-form-item label="Secret" prop="secret">
          <el-input v-model="ruleForm.secret" maxlength="20" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(formRef)">保存</el-button>
          <el-button @click="resetForm(formRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from 'vue'
import { getConfig, saveOrUpdateConfig } from '@/api/server'

const formRef = ref(null)
const ruleForm = ref({
  appId: '',
  secret: '',
})
const rules = {
  appId: [{ required: true, message: '请输入服务ID', trigger: 'blur' }],
  secret: [{ required: true, message: '请输入Secret', trigger: 'blur' }],
}

onBeforeMount(() => {
  getConfigInfo()
})

// 获取客服配置
const getConfigInfo = () => {
  getConfig().then((res) => {
    ruleForm.value = res.data
  })
}
// 保存
const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      saveOrUpdateConfig(ruleForm.value).then((res) => {
        if (res.code === '') {
          ElMessage.success('保存成功')
        }
      })
    } else {
      return false
    }
  })
}
const resetForm = (formEl) => {
  formEl.resetFields()
}
</script>

<style lang="scss" scpoed>
.servicePage {
  padding: 5rem 20px;
  .configuration {
    width: 600px;
  }
}
</style>
